﻿using Common.DataModels;
using Extensions.Attribute.TransactionAttribute;
using Microsoft.AspNetCore.Mvc;
using PLC.Domain.Dto;
using PLC.Domain.Enum;
using PLC.Domain.Service.Service;

namespace PLC.API.Controllers
{
    [Route("api/production-data")]
    [ApiController]
    public class ProductionDataController : BaseApiController
    {
        private readonly ProductionDataService _productionDataService;

        public ProductionDataController(ProductionDataService productionDataService)
        {
            _productionDataService = productionDataService?? throw new ArgumentNullException(nameof(productionDataService));
        }

        /// <summary>
        /// 精确查询
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet("{id}")]
        public async Task<MessageModel<DtoProductionData>> FindOne([FromRoute] long id)
        {
            var data = await _productionDataService.FindOneAsync(id);
            return Success(data, "查询成功");
        }
        /// <summary>
        /// 分页查询(这边组装由java那边负责  我这里返回最基础的数据就好了)
        /// </summary>
        /// <param name="pagingModel"></param>
        /// <param name="startTime"></param>
        /// <param name="endTime"></param>
        /// <param name="taskCode"></param>
        /// <param name="upperEndPlateCode"></param>
        /// <param name="lowerEndPlateCode"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<MessageModel<List<DtoProductionData>>> FindByPage([FromQuery] PagingModel pagingModel,  DateTime startTime, DateTime endTime, string taskCode="", string upperEndPlateCode="", string lowerEndPlateCode="")
        {
            var data = await _productionDataService.FindByPageAsync(pagingModel,taskCode,upperEndPlateCode,lowerEndPlateCode , startTime, endTime);
            return Success(data.Item1, data.Item2, "查询成功");
        }
        /// <summary>
        /// 获取装堆机压力过程数据
        /// </summary>
        /// <param name="page"></param>
        /// <param name="rows"></param>
        /// <param name="sort"></param>
        /// <param name="upperEndPlateCode"></param>
        /// <param name="lowerEndPlateCode"></param>
        /// <returns></returns>
        [HttpGet("process/stacker")]
        public async Task<MessageModel<IList<DtoPressureData>>> FindStackerDataByCodeAsync(int page=1, int rows=10,
            string sort="",
            string upperEndPlateCode="", string lowerEndPlateCode="")
        {
            var (data, count) =
                await _productionDataService.FindStackerDataByCodeAsync(page, rows, sort, upperEndPlateCode,
                    lowerEndPlateCode);
            return Success(data, count);
        }

        /// <summary>
        /// 查询膜电极和双极板的条码信息
        /// </summary>
        /// <param name="pagingModel"></param>
        /// <param name="upperEndPlateCode"></param>
        /// <param name="lowerEndPlateCode"></param>
        /// <param name="deviceType"></param>
        /// <returns></returns>
        [HttpGet("bar-codes")]
        public async Task<MessageModel<IList<DtoBarcodeData>>> FindBarcodeData([FromQuery] PagingModel pagingModel,
            string upperEndPlateCode = "", string lowerEndPlateCode = "", int deviceType=0)
        {
            var (data,count) = await _productionDataService.FindBarCodeByCodeAsync(pagingModel.page,pagingModel.rows,pagingModel.sort,upperEndPlateCode,lowerEndPlateCode, deviceType);
            return Success(data,count);
        }
        /// <summary>
        /// 根据设备类型和上下端板编号查询最新的一条结果数据
        /// </summary>
        /// <param name="deviceType"></param>
        /// <param name="upperEndPlateCode"></param>
        /// <param name="lowerEndPlateCode"></param>
        /// <returns></returns>
        [HttpGet("device-type")]
        public async Task<MessageModel<DtoProductionData>> FindFindOneByDeviceType(int deviceType,
            string upperEndPlateCode="",
            string lowerEndPlateCode="")
        {
            var data = await _productionDataService.FindOneByDeviceType(deviceType, upperEndPlateCode,
                lowerEndPlateCode);
            return Success(data);
        }
        /// <summary>
        /// 查询设备状态
        /// </summary>
        /// <param name="pagingModel"></param>
        /// <param name="deviceId"></param>
        /// <param name="starTime"></param>
        /// <param name="endTime"></param>
        /// <returns></returns>
        //[HttpGet("device-status")]
        //public async Task<MessageModel<List<DtoDeviceStatus>>> FindDeviceStatus([FromQuery] PagingModel pagingModel,long deviceId,DateTime starTime,DateTime endTime)
        //{
        //    var data = await _productionDataService.FindDeviceStatus(pagingModel, deviceId, starTime, endTime);
        //    return Success(data.Item1, data.Item2);
        //}
        /// <summary>
        /// 创建
        /// </summary>
        /// <param name="dtoProductionData"></param>
        /// <returns></returns>
        [HttpPost]
        [Transaction]
        public async Task<MessageModel<DtoProductionData>> Create([FromBody] DtoProductionData dtoProductionData)
        {
            var data = await _productionDataService.CreateAsync(dtoProductionData);
            return Success(data, "创建成功");
        }
        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="dtoProductionData"></param>
        /// <returns></returns>
        [HttpPut]
        [Transaction]
        public async Task<MessageModel<DtoProductionData>> Update([FromBody] DtoProductionData dtoProductionData)
        {
            var data = await _productionDataService.UpdateAsync(dtoProductionData);
            return Success(data, "修改成功");
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        [HttpDelete]
        [Transaction]
        public async Task<MessageModel> Delete([FromBody] List<long> ids)
        {
            await _productionDataService.DeleteAsync(ids);
            return Success("删除成功");
        }
    }
}
